package com.itheima.juc23.question1;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author li
 * 一共有1000张电影票，可以在两个窗口领取，假设每次领取的时间为3000ms
 *  * 要求：请用多线程模拟卖票过程并打印剩余电影票的数量
 */
public class MyRunnable implements Runnable {
    int count = 1000;

    Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            lock.lock();
            try {
                if (count == 0) {
                    break;
                } else {
                    Thread.sleep(10);
                    count--;
                    System.out.println(Thread.currentThread().getName() + "正在卖票，剩余票：" + count);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }
    }
}
